#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <libxml/xmlmemory.h> 
#include <libxml/parser.h> 

#include <sys/statfs.h>

#define SERV_PORT 10000
#define MEDIA_UPDATE_PORT 9501
#define DEBUG 1

typedef struct media_play_info{
	
	unsigned char media_play_dir[20];
	unsigned char media_play_filename[40];
}MEDIAPLAYINFO;

MEDIAPLAYINFO meida_info[200];
char tmp_list[40][40];
int tmp_num = 0;

int media_num=0;
int media_isnot_empty=0;
int download_flag=0;

char weather_md5[40];
char weather_md5_bak[40];


#define F(x,y,z) (((x)&(y))|((~x)&(z)))
#define G(x,y,z) (((x)&(z))|((y)&(~z)))
#define H(x,y,z) ((x)^(y)^(z))
#define I(x,y,z) ((y)^((x)|(~z)))
#define RL(x,y) (((x)<<(y))|((x)>>(32-(y))))//x向左循环移y位
#define PP(x) (x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24)//将x高低位互换,例如PP(aabbccdd)=ddccbbaa
#define FF(a,b,c,d,x,s,ac) a=b+(RL((a+F(b,c,d)+x+ac),s))
#define GG(a,b,c,d,x,s,ac) a=b+(RL((a+G(b,c,d)+x+ac),s))
#define HH(a,b,c,d,x,s,ac) a=b+(RL((a+H(b,c,d)+x+ac),s))
#define II(a,b,c,d,x,s,ac) a=b+(RL((a+I(b,c,d)+x+ac),s))
unsigned int A,B,C,D,a,b,c,d,i,j,len,flen[2],x[16],x1[1024*32];//i临时变量,len文件长,flen[2]为64位二进制表示的文件初始长度
char *str;
FILE *fp;

void md5(){
a=A,b=B,c=C,d=D;

FF(a,b,c,d,x[0],7,0xd76aa478);
FF(d,a,b,c,x[1],12,0xe8c7b756);
FF(c,d,a,b,x[2],17,0x242070db);
FF(b,c,d,a,x[3],22,0xc1bdceee);
FF(a,b,c,d,x[4],7,0xf57c0faf);
FF(d,a,b,c,x[5],12,0x4787c62a);
FF(c,d,a,b,x[6],17,0xa8304613);
FF(b,c,d,a,x[7],22,0xfd469501);
FF(a,b,c,d,x[8],7,0x698098d8);
FF(d,a,b,c,x[9],12,0x8b44f7af);
FF(c,d,a,b,x[10],17,0xffff5bb1);
FF(b,c,d,a,x[11],22,0x895cd7be);
FF(a,b,c,d,x[12],7,0x6b901122);
FF(d,a,b,c,x[13],12,0xfd987193);
FF(c,d,a,b,x[14],17,0xa679438e);
FF(b,c,d,a,x[15],22,0x49b40821);

/**//*Round2*/
GG(a,b,c,d,x[1],5,0xf61e2562);
GG(d,a,b,c,x[6],9,0xc040b340);
GG(c,d,a,b,x[11],14,0x265e5a51);
GG(b,c,d,a,x[0],20,0xe9b6c7aa);
GG(a,b,c,d,x[5],5,0xd62f105d);
GG(d,a,b,c,x[10],9,0x02441453);
GG(c,d,a,b,x[15],14,0xd8a1e681);
GG(b,c,d,a,x[4],20,0xe7d3fbc8);
GG(a,b,c,d,x[9],5,0x21e1cde6);
GG(d,a,b,c,x[14],9,0xc33707d6);
GG(c,d,a,b,x[3],14,0xf4d50d87);
GG(b,c,d,a,x[8],20,0x455a14ed);
GG(a,b,c,d,x[13],5,0xa9e3e905);
GG(d,a,b,c,x[2],9,0xfcefa3f8);
GG(c,d,a,b,x[7],14,0x676f02d9);
GG(b,c,d,a,x[12],20,0x8d2a4c8a);

/**//*Round3*/
HH(a,b,c,d,x[5],4,0xfffa3942);/**//*33*/
HH(d,a,b,c,x[8],11,0x8771f681);/**//*34*/
HH(c,d,a,b,x[11],16,0x6d9d6122);/**//*35*/
HH(b,c,d,a,x[14],23,0xfde5380c);/**//*36*/
HH(a,b,c,d,x[1],4,0xa4beea44);/**//*37*/
HH(d,a,b,c,x[4],11,0x4bdecfa9);/**//*38*/
HH(c,d,a,b,x[7],16,0xf6bb4b60);/**//*39*/
HH(b,c,d,a,x[10],23,0xbebfbc70);/**//*40*/
HH(a,b,c,d,x[13],4,0x289b7ec6);/**//*41*/
HH(d,a,b,c,x[0],11,0xeaa127fa);/**//*42*/
HH(c,d,a,b,x[3],16,0xd4ef3085);/**//*43*/
HH(b,c,d,a,x[6],23,0x04881d05);/**//*44*/
HH(a,b,c,d,x[9],4,0xd9d4d039);/**//*45*/
HH(d,a,b,c,x[12],11,0xe6db99e5);/**//*46*/
HH(c,d,a,b,x[15],16,0x1fa27cf8);/**//*47*/
HH(b,c,d,a,x[2],23,0xc4ac5665);/**//*48*/

/**//*Round4*/
II(a,b,c,d,x[0],6,0xf4292244);/**//*49*/
II(d,a,b,c,x[7],10,0x432aff97);/**//*50*/
II(c,d,a,b,x[14],15,0xab9423a7);/**//*51*/
II(b,c,d,a,x[5],21,0xfc93a039);/**//*52*/
II(a,b,c,d,x[12],6,0x655b59c3);/**//*53*/
II(d,a,b,c,x[3],10,0x8f0ccc92);/**//*54*/
II(c,d,a,b,x[10],15,0xffeff47d);/**//*55*/
II(b,c,d,a,x[1],21,0x85845dd1);/**//*56*/
II(a,b,c,d,x[8],6,0x6fa87e4f);/**//*57*/
II(d,a,b,c,x[15],10,0xfe2ce6e0);/**//*58*/
II(c,d,a,b,x[6],15,0xa3014314);/**//*59*/
II(b,c,d,a,x[13],21,0x4e0811a1);/**//*60*/
II(a,b,c,d,x[4],6,0xf7537e82);/**//*61*/
II(d,a,b,c,x[11],10,0xbd3af235);/**//*62*/
II(c,d,a,b,x[2],15,0x2ad7d2bb);/**//*63*/
II(b,c,d,a,x[9],21,0xeb86d391);/**//*64*/

A+=a;
B+=b;
C+=c;
D+=d;

}

int md5summ(char *file_tmp)
{
	printf("\nstart:");
	system("date");
	printf("\n");
	char MD5Code[100];
	memset(MD5Code,0,100);
	if(!(fp=fopen(file_tmp,"rb")))
	{
		printf("Cannotopenthisfile!\n");
		return -1;
	}//以二进制打开文件
	fseek(fp,0,SEEK_END);//文件指针转到文件末尾
	if((len=ftell(fp))==-1)
	{
		printf("Sorry!Cannotcalculatefileswhichlargerthan2GB!\n");
		fclose(fp);
		return -1;
	}//ftell函数返回long,最大为2GB,超出返回-1
	rewind(fp);//文件指针复位到文件头
	A=0x67452301,B=0xefcdab89,C=0x98badcfe,D=0x10325476;//初始化链接变量
	flen[1]=len/0x20000000;//flen单位是bit
	flen[0]=(len%0x20000000)*8;


	memset(x1,0,1024*128);
	for(i=0;i<len/(1024*128);i++)
	{
		fread(&x1,4,1024*32,fp);
		memset(x,0,64);//初始化x数组为0
		str = (char*)x1;
		memcpy(x,str,64);//以4字节为一组,读取16组数据
		for(j=0;j<1024*2;j++)
		{//循环运算直至文件结束
			md5();
			memset(x,0,64);
			str=str+64;
			memcpy(x,str,64);
		}
	//usleep(1);
	}

	len = len%(1024*128);

	memset(x,0,64);//初始化x数组为0
	fread(&x,4,16,fp);//以4字节为一组,读取16组数据
	for(i=0;i<len/64;i++){//循环运算直至文件结束
	md5();
	memset(x,0,64);
	fread(&x,4,16,fp);
	//usleep(1);
	}
	((char*)x)[len%64]=128;//文件结束补1,补0操作,128二进制即10000000
	if(len%64>55)md5(),memset(x,0,64);
	memcpy(x+14,flen,8);//文件末尾加入原文件的bit长度
	md5();


	fclose(fp);
	printf("\nstop:");
	system("date");
	printf("\n");
	sprintf(MD5Code,"%08x%08x%08x%08x",PP(A),PP(B),PP(C),PP(D));//高低位逆反输出
	if(strstr(file_tmp,".bmp")!=NULL)
	{
		memset(weather_md5,0,40);
		sprintf(weather_md5,"%08x%08x%08x%08x",PP(A),PP(B),PP(C),PP(D));
		return 0;
	}
		
	if(strstr(file_tmp,MD5Code)==NULL)
	return -1;
	else
	return 2;
	
}

int rm_dele(char* cmd)
{
    pid_t child, status;
    if ((child = fork()) < 0) {
        perror("fork");
        return 1;
    }
    if(child == 0){
        execlp("rm" , "rm" , "-rf" , cmd ,  NULL);
        return -1;
    } else {
        if(wait(&status) < 0) {
            perror("wait");
            exit(1);
        }
    }
    return status;
}

int zip_password_check(char *argv)
{
	char buf[10];
	int len = 0;
	memset(buf,0,10);
	FILE * fd = fopen(argv,"rb");
	if(fd == NULL)
	{
		printf("file is not exist\n");
		return 0;
	}
	len = fread(buf,1,10,fd);
	fclose(fd);
	if(len > 0)
	{
			if(buf[6]==0x0)
				return 0;
			else if(buf[6]==0x1)
				return 1;
	}
	
	return 0;
	
}

int get_tmp_list()
{
	DIR *pdir = NULL;
	struct dirent *ptr = NULL;
	
	int i;
	
	for(i=0;i<40;i++)
	{
		memset(tmp_list[i],0,40);
	}
	
	i = 0;
	tmp_num=0;
	pdir = opendir("/mnt/sdcard/VIDEO/");
	if(pdir==NULL)
	{
		return 1;
	}
	while((ptr=readdir(pdir))!=NULL)
	{
		if(strstr(ptr->d_name, ".tmp"))
		{			
			strcpy(tmp_list[tmp_num], ptr->d_name);			
			tmp_num++;
			if(tmp_num>=40)
				break;
		}
	}
	closedir(pdir);
	
}

int wget_src0(char* cmd)
{
    char cmd1[100];
    memset(cmd1,0,100);
    strcpy(cmd1,cmd);
    pid_t child, status;
    //pid = getpid();
    if ((child = fork()) < 0) {
        perror("fork");
        return 1;
    }
    if(child == 0){
	//alarm(12);
        execlp("./wget" , "wget" , "-N" ,"--limit-rate=200k","-P","./",cmd , NULL);
    } else {
	//sleep(10);
	//kill(child,SIGKILL);
        if(wait(&status) < 0) {
			kill(child,SIGKILL);
            perror("wait");
            return -1;
        }
	
    }
    return status;
}

int wget_src(char* cmd,char*str)
{
    char cmd1[200];
    char addr1[200];
    char addr2[200];
    memset(cmd1,0,200);
    strcpy(cmd1,str);
    strcat(cmd1,".tmp");
    pid_t child, status;
    //pid = getpid();
    download_flag = 1;
    if ((child = fork()) < 0) {
        perror("fork");
        return 1;
    }
    if(child == 0){
	//alarm(12);
        execlp("./wget" , "wget" ,"-c","--limit-rate=200k","-P","./" , cmd , "-O", cmd1, NULL);
        return 0;
    } 
    else
    {
    	sleep(120);
    	kill(child,SIGKILL);
        wait(&status);
        if(WIFEXITED(status))
	{/*如果WIFEXITED(status）返回非零值*/	
		memset(addr1,0,200);
		strcpy(addr1,str);
		strcat(addr1,".tmp");
		if(access(addr1, 0) == 0)
		{
			if(md5summ(addr1)==2)
			{
				memset(cmd1, 0, 200);
				strcpy(cmd1,"mv ");
				strcat(cmd1,addr1);
				strcat(cmd1," ");
				strcat(cmd1,str);
				system(cmd1);

				download_flag = 0;

				if( strstr(str,".bin")!=NULL )
				{
					download_flag = 1;
					memset(addr2, 0, 100);
					strcpy(addr2,"cp ");
					strcat(addr2,str);
					strcat(addr2," /mnt/sdcard/firmware.bin -f");
					system(addr2);
					
					system("mkdir -p /mnt/sdcard/LIFTUPDATE/LIFT");
					system("mv /mnt/sdcard/firmware.bin /mnt/sdcard/LIFTUPDATE/LIFT/ -f");
					system("sync");

					sleep(2);
					system("reboot");

				}
				else if( strstr(str,".zip")!=NULL )
				{
					download_flag = 1;
					int a=0;
					a=zip_password_check(str);
					printf("zip_password_check result is %d\n",a);
					if(1)
					{
					
						memset(addr2, 0, 100);
						strcpy(addr2,"mv ");
						strcat(addr2,str);
						strcat(addr2," /mnt/sdcard/LIFT.zip -f");
						system(addr2);
						system("mkdir -p /mnt/sdcard/LIFTUPDATE");
						system("/mnt/appimg/unzip -P mux /mnt/sdcard/LIFT.zip -d /mnt/sdcard/LIFTUPDATE/");
						
						//system("rm *.zip -rf");
						
						memset(addr2, 0, 100);
						strcpy(addr2,"mv -f ");						
						strcat(addr2,"/mnt/sdcard/LIFT.zip ");
						strcat(addr2,str);
						system(addr2);
						
						
						system("sync");
						sleep(2);
						system("reboot");
					}

				}
			}
			else
			{
				rm_dele(addr1);
				system("sync");				
			}
		}
		return status;
	}
	else/*如果WIFEXITED(status）返回零值*/
		return status;
    }
}


getReference(xmlNodePtr cur)
{
	char* value=NULL;
	cur=cur->xmlChildrenNode;

	while(cur !=NULL)
	{
		if(  !xmlStrcmp("Stream",(char*)(cur->name)) || (!xmlStrcmp("WeatherWidth",(char*)(cur->name))))
		{
			cur=cur->next;
			continue;
		}

		if( (!xmlStrcmp("Video",(char*)(cur->name))) || (!xmlStrcmp("Weather",(char*)(cur->name))) || (!xmlStrcmp("Software",(char*)(cur->name)))\
		    || (!xmlStrcmp("Text",(char*)(cur->name))) || (!xmlStrcmp("Lift",(char*)(cur->name))))
		{		
			media_num++;
			memset(meida_info[media_num-1].media_play_dir,0,20);			
			sprintf(meida_info[media_num-1].media_play_dir,"%s",(char*)(cur->name));
			if(DEBUG)
			printf("\ndir is %s\n",meida_info[media_num-1].media_play_dir);	
			
		}

		if( (!xmlStrcmp("FileName",(char*)(cur->name))) )
		{
			memset(meida_info[media_num-1].media_play_filename,0,40);
			value=(char*)xmlNodeGetContent(cur);		
				sprintf(meida_info[media_num-1].media_play_filename,"%s",value);
			xmlFree(value);			
			if(DEBUG)
			printf("FileName is: %s\n",meida_info[media_num-1].media_play_filename);
		}
		
		getReference(cur);
		cur=cur->next; 
	}
}

int get_list(char*cmd)
{
	int n;
	xmlDocPtr doc=NULL;
	xmlNodePtr cur=NULL;

	xmlKeepBlanksDefault (0);//忽略空白行 
	
	doc=xmlParseFile(cmd);//创建Dom树
	if(doc==NULL)
	{
		printf("Loading xml file failed.\n");
		return 1; 
	}
	cur=xmlDocGetRootElement(doc);//获取根节点
	if(cur==NULL)
	{
		printf("empty file\n");
		xmlFreeDoc(doc); 
		return 2; 
	}

	media_num=0;
	getReference(cur);
	xmlFreeDoc(doc);	
	n=media_num;
	return 0;

}


getReference1(xmlNodePtr cur)
{
	char* value=NULL;
	cur=cur->xmlChildrenNode;

	while(cur !=NULL)
	{
		if( (!xmlStrcmp("Video",(char*)(cur->name))) || (!xmlStrcmp("Stream",(char*)(cur->name))) )
		{		
			media_isnot_empty++;
		}		
		getReference(cur);
		cur=cur->next; 
	}
}

int media_list_is_empty(char*cmd)
{
	int n;
	xmlDocPtr doc=NULL;
	xmlNodePtr cur=NULL;

	media_isnot_empty=0;
	xmlKeepBlanksDefault (0);//忽略空白行 
	
	doc=xmlParseFile(cmd);//创建Dom树
	if(doc==NULL)
	{
		printf("Loading xml file failed.\n");
		return 1; 
	}
	cur=xmlDocGetRootElement(doc);//获取根节点
	if(cur==NULL)
	{
		printf("empty file\n");
		xmlFreeDoc(doc); 
		return 2; 
	}

	getReference1(cur);
	xmlFreeDoc(doc);	
	n=media_isnot_empty;
	return 0;

}

int sdcard_exist()
{
	struct statfs diskInfo;
	
	statfs("/mnt/sdcard", &diskInfo);
	unsigned long long blocksize = diskInfo.f_bsize;	//每个block里包含的字节数
	unsigned long long totalsize = blocksize * diskInfo.f_blocks; 	//总的字节数，f_blocks为block的数目
	
	printf("Total_size = %llu MB\n", totalsize>>20);
	
	if(totalsize==0)
		return 0;
	else
		return 1;
	
	/*printf("Total_size = %llu B = %llu KB = %llu MB = %llu GB\n", 
		totalsize, totalsize>>10, totalsize>>20, totalsize>>30);
	
	unsigned long long freeDisk = diskInfo.f_bfree * blocksize;	//剩余空间的大小
	unsigned long long availableDisk = diskInfo.f_bavail * blocksize; 	//可用空间大小
	printf("Disk_free = %llu MB = %llu GB\nDisk_available = %llu MB = %llu GB\n", 
		freeDisk>>20, freeDisk>>30, availableDisk>>20, availableDisk>>30);
	*/
}

int main()
{
	FILE * dele_fd;
	FILE *ipid_fd;
	char dele_cmd[160];
	char dir[100];
	char addr[100];
	char server_ip[40];
	char client_id[40];
	char *s;

	char *first;
	char *end;

	struct sockaddr_in servaddr;
	int sockfd;
	char *buf;
	int mdn;
	//char str[INET_ADDRSTRLEN];
	//socklen_t servaddr_len;
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
	servaddr.sin_port = htons(MEDIA_UPDATE_PORT);
	buf = (char*)malloc(100);

	int sockfd1;
	struct sockaddr_in servaddr1;
	sockfd1 = socket(AF_INET, SOCK_DGRAM, 0);
	bzero(&servaddr1, sizeof(servaddr1));
	servaddr1.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", &servaddr1.sin_addr);
	servaddr1.sin_port = htons(6666);
	char cdl[100];//currentdownload

	memset(client_id,0,40);
	ipid_fd=fopen("/v4asys/mac","r");
	if(ipid_fd!=NULL)
	{	     
		fgets(client_id,40,ipid_fd);
		fclose(ipid_fd);
	}
	
	memset(dir, 0, 100);	
	strcpy(dir,client_id);
	strcat(dir,".xml");
	
	if( access(dir,0)!=0 )
	{
		memset(dele_cmd, 0, 160);
		strcpy(dele_cmd,"touch ");
		strcat(dele_cmd,client_id);
		strcat(dele_cmd,".xml");
		system(dele_cmd);
		system("sync");
	}
	
	memset(weather_md5,0,40);
	memset(weather_md5_bak,0,40);
	
	while(1)
	{
		get_tmp_list();
		for(i=0;i<tmp_num;i++)
		{
			for(j=0;j<media_num;j++)
			{
				if(strstr(tmp_list[i],meida_info[j].media_play_filename))
				{
					j=0;
					break;
				}
			}
			if(j==media_num)
			{
				rm_dele(tmp_list[i]);
			}
			
		}
		
		while( access("/mnt/sdcard/LIFT", 0) == 0)
		{
			sleep(5);
		}	
			
		while(sdcard_exist()==0)
		{
			sleep(5);
		}
			
			
		memset(server_ip,0,40);

		ipid_fd=fopen("/v4asys/server_ip","r");
		if(ipid_fd!=NULL)
		{	     
			fgets(server_ip,40,ipid_fd);
			fclose(ipid_fd);
		}
		
		system("touch native.xml");

		strcpy(addr, "ftp://");
		strcat(addr,server_ip);
		strcat(addr, "/Script/");
		strcat(addr,client_id);
		strcat(addr, ".xml");
		wget_src0(addr);
		
		memset(dele_cmd, 0, 160);
		strcpy(dele_cmd, "awk -F\"|\" 'NR==FNR{a[$0]}NR!=FNR&&!($1 in a){print $0}' ");
		strcat(dele_cmd,client_id);
		strcat(dele_cmd,".xml");
		strcat(dele_cmd," native.xml | grep FileName > dele");
		system(dele_cmd);
		
		memset(dele_cmd, 0, 160);
		strcpy(dele_cmd,"diff ");
		strcat(dele_cmd,client_id);
		strcat(dele_cmd,".xml");
		strcat(dele_cmd," native.xml > media_update");
		system(dele_cmd);
		system("sync");
		
		memset(dir, 0, 100);
		strcpy(dir,client_id);
		strcat(dir,".xml");
		media_list_is_empty(dir);

	
		memset(dir, 0, 100);
		strcpy(dir,client_id);
		strcat(dir,".xml");
		get_list(dir);

		for(mdn = 0; mdn < media_num; mdn++)
		{
			memset(addr, 0, 100);
			memset(dir, 0, 100);
			strcpy(addr, "ftp://");
			strcat(addr,server_ip);
			if( (strstr("Weather", meida_info[mdn].media_play_dir)!=NULL) || (strstr("Text", meida_info[mdn].media_play_dir)!=NULL) )//如果是weather文件或文本文件
			{
				strcat(addr,"/");
				strcat(addr,meida_info[mdn].media_play_dir);
				strcat(addr,"/");
				strcat(addr,meida_info[mdn].media_play_filename);
				wget_src0(addr);
				continue;
			}

			if(access(meida_info[mdn].media_play_filename, 0) == 0)//如果该文件存在
			{
				continue;
			}
			
			if(strstr("Firmware",meida_info[mdn].media_play_filename))
				continue;

			memset(cdl,0,100);
			strcpy(cdl," ");
			strcat(cdl,meida_info[mdn].media_play_filename);
			strcat(cdl,".currentdownload");
			sendto(sockfd1, cdl, strlen(cdl), 0, (struct sockaddr *)&servaddr1, sizeof(servaddr1));

			strcat(addr,"/");
			strcat(addr,meida_info[mdn].media_play_dir);
			strcat(addr,"/");
			strcat(addr,meida_info[mdn].media_play_filename);


			wget_src(addr,meida_info[mdn].media_play_filename);
			system("sync");
			
			buf = " .currentdownload";//download end
			sendto(sockfd1, buf, strlen(buf), 0, (struct sockaddr *)&servaddr1, sizeof(servaddr1));

			if( (media_isnot_empty>0)&&(download_flag==0) )
			{
				if((dele_fd = fopen("dele","r"))==NULL)
					printf("open dele erro\n");
				memset(dir, 0, 100);
				memset(addr, 0, 100);
				while(fgets(dir,100,dele_fd)!=NULL)
				{
					if((first = strstr(dir,"<FileName>"))!=NULL)
					{
						end = strstr(dir,"</FileName>");
						memcpy(addr, first+10, end-first-10);
					}

					rm_dele(addr);

					memset(dir, 0, 100);
					memset(addr, 0, 100);
				}
				fclose(dele_fd);
		
				memset(dele_cmd, 0, 160);
				strcpy(dele_cmd,"cp ");
				strcat(dele_cmd,client_id);
				strcat(dele_cmd,".xml");
				strcat(dele_cmd," native.xml");
				system(dele_cmd);		
		
				if((dele_fd = fopen("media_update","r"))==NULL)
				printf("open media erro\n");
				memset(dir, 0, 100);
		
				if(fread(dir,1,100,dele_fd)>1)//文件不为空，列表有更新
				{
					buf = "media";
					sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));	
				}
				fclose(dele_fd);
				media_isnot_empty=0;
			}
		}

	if( (media_isnot_empty>0)&&(download_flag==0) )
	{
		if((dele_fd = fopen("dele","r"))==NULL)
			printf("open dele erro\n");
		memset(dir, 0, 100);
		memset(addr, 0, 100);
		while(fgets(dir,100,dele_fd)!=NULL)
		{
			if((first = strstr(dir,"<FileName>"))!=NULL)
			{
				end = strstr(dir,"</FileName>");
				memcpy(addr, first+10, end-first-10);
			}

			rm_dele(addr);

			memset(dir, 0, 100);
			memset(addr, 0, 100);
		}
		fclose(dele_fd);
		
		memset(dele_cmd, 0, 160);
		strcpy(dele_cmd,"cp ");
		strcat(dele_cmd,client_id);
		strcat(dele_cmd,".xml");
		strcat(dele_cmd," native.xml");
		system(dele_cmd);		
		
		if((dele_fd = fopen("media_update","r"))==NULL)
		printf("open media erro\n");
		memset(dir, 0, 100);
		
		if(fread(dir,1,100,dele_fd)>1)//文件不为空，列表有更新
		{
			buf = "media";
			sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));	
		}
		fclose(dele_fd);
	}
	
	
	if( 1 )
	{
		if((dele_fd = fopen("dele","r"))==NULL)
			printf("open dele erro\n");
		memset(dir, 0, 100);
		memset(addr, 0, 100);
		while(fgets(dir,100,dele_fd)!=NULL)
		{
			if(strstr(dir,".txt")!=NULL)
			if((first = strstr(dir,"<FileName>"))!=NULL)
			{
				end = strstr(dir,"</FileName>");
				memcpy(addr, first+10, end-first-10);
			}

			rm_dele(addr);

			memset(dir, 0, 100);
			memset(addr, 0, 100);
		}
		fclose(dele_fd);
		
	}
	
		sleep(5);
		memset(addr, 0, 100);
		strcpy(addr,client_id);
		strcat(addr,".bmp");		
		md5summ(addr);
		if( strcmp(weather_md5_bak,weather_md5) != 0)
		{
			memset(addr, 0, 100);
			strcpy(addr,"cp ");
			strcat(addr,client_id);
			strcat(addr,".bmp /mnt/sdcard/weather.bmp -f");
			system(addr);
			memset(weather_md5_bak,0,40);
			memcpy(weather_md5_bak,weather_md5,40);
		}

		sleep(10);
		
	}
	free(buf);
	close(sockfd);
	close(sockfd1);
	return 0;
}
